# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Carlo Landmeter <clandmeter@alpinelinux.org>
pkgname=emby
pkgver=3.0.5911
pkgrel=4
pkgdesc="Bring together your videos, music, photos, and live television"
url="https://emby.media"
arch="x86_64"
license="GPL-2.0"
depends="ffmpeg mono imagemagick sqlite"
pkgusers=$pkgname
pkggroups=$pkgname
depends_dev=""
makedepends="$depends_dev"
install="$pkgname.pre-install"
subpackages="$pkgname-migrate"
source="Emby-$pkgver.tar.gz::https://github.com/MediaBrowser/Emby/archive/$pkgver.tar.gz
	emby-server
	$pkgname.initd
	$pkgname.confd
	emby-migrate"

_builddir="$srcdir"/Emby-$pkgver

prepare() {
	local i
	local libMagicWand="libMagickWand-7.Q16HDRI.so.8"
	cd "$_builddir"
	for i in $source; do
		case $i in
		*.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;;
		esac
	done
	mkdir -p "$_builddir"/build
	if [ -e "/usr/lib/$libMagicWand" ]; then
		sed -i s/libMagickWand-6.Q8.so/$libMagicWand/g \
			"$_builddir"/MediaBrowser.Server.Mono/ImageMagickSharp.dll.config \
			|| return 1
	else
		die "libMagickWand not found!"
	fi
	if [ -e "/usr/lib/libsqlite3.so.0" ]; then
		sed -i s/libsqlite3.so/libsqlite3.so.0/g \
			"$_builddir"/MediaBrowser.Server.Mono/System.Data.SQLite.dll.config \
			|| return 1
	else
		die "libsqlite3 not found!"
	fi
}

build() {
	cd "$_builddir"
	xbuild \
		/p:Configuration='Release Mono' \
		/p:Platform='Any CPU' \
		/p:OutputPath="$_builddir"/build \
		/t:build MediaBrowser.Mono.sln || return 1
}

package() {
	cd "$_builddir"
	mkdir -p "$pkgdir"/usr/lib/emby || return 1
	install -d -o $pkgusers -g $pkggroups \
		 "$pkgdir"/var/lib/emby || return 1
	cp -r "$_builddir"/build/* \
		"$pkgdir"/usr/lib/emby/
	install -Dm755 "$srcdir"/emby-server \
		"$pkgdir"/usr/bin/emby-server || return 1
	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/emby || return 1
	install -Dm644 "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/emby || return 1
}

migrate() {
	pkgdesc="$pkgname database migration script"
	depends="sqlite"
	install -Dm755 "$srcdir"/emby-migrate \
		"$subpkgdir"/usr/bin/emby-migrate || return 1
}

sha512sums="ec48ee8d8403074b91d0dfc90cf463bab805c8b59f06d8e9035a166b1a41d8b4f2c22d007f17b763c902e655c34c3dc01f73bedfe9ada7592e72fd3f96513e39  Emby-3.0.5911.tar.gz
f161fbf2d83d91e5436578f1998410dba991c8d2de4fb8852ec106b13ce3e7119fec7432998dfffa2c488425e2a5eacaf34b0c4e6fc93ad370c1ba94b3eaa2e4  emby-server
2a520ee646b0489bbacd79679e75828194cbcd41845948816d1ae4d129f7cd582561f7357fa0c6d7f3f1970cc7900ae34338d54a4e54d5b21284c020a911e284  emby.initd
ebdb39d3e17277ecc51709fc6862265295892d6aa1b5f1b9da537a80552b81c845863cd058ab468f7cf20c22f8f56954b7cce4d149f8fa233ea19ae7aa0bc001  emby.confd
d4275622b26228c352ee9150441995c2062f6afdda46ccca176ad13a17d5ce432f6cf80b7c7d3029763d3e6fb79cf9a7a07bdbf4fef1dfaee2811c56ec0ec7b9  emby-migrate"
